/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cip.esymons.tesoreria.beans;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRXmlExporter;
import net.sf.jasperreports.engine.util.JRLoader;
import org.cip.esymons.general.util.ControladorAbstracto;
import org.cip.esymons.tesoreria.bc.ComprobanteEgresoFacadeLocal;
import org.cip.esymons.tesoreria.bc.DetComegresoFacadeLocal;
import org.cip.esymons.tesoreria.be.ComprobanteEgreso;
import org.cip.esymons.tesoreria.be.DetComegreso;

/**
 *
 * @author Gilmar
 */
public class ReporteVoucher extends HttpServlet {

    @EJB
    ComprobanteEgresoFacadeLocal comprobanteEgresoFacadeLocal;
    @EJB
    DetComegresoFacadeLocal detComegresoFacadeLocal;
    private String PREFIX_W = "C:\\cip\\reportes\\";
    private String PREFIX_L = "/modulos/reportes/jaspers/";

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            Long codigoDocumento = Long.valueOf(request.getParameter("codVoucher").toString());
            reporteBoleta(comprobanteEgresoFacadeLocal.find(codigoDocumento), response);
        } catch (Exception e) {
            PrintWriter out = response.getWriter();
            out.println("ER");
            out.close();
            ControladorAbstracto.error(e.getMessage());
        }
    }

    private void reporteBoleta(ComprobanteEgreso comprobanteEgreso, HttpServletResponse response) throws JRException, IOException {
        boolean os = System.getProperty("os.name").contains("Windows");
        String nombreReporte = "";
        String ruta = "";
        if (os) {
            ruta = PREFIX_W;
        } else {
            ruta = PREFIX_L;
        }
        String cadenaComprobante = "";
        for (DetComegreso dce : detComegresoFacadeLocal.buscarPorComprobanteEgreso(comprobanteEgreso)) {
            cadenaComprobante = cadenaComprobante + dce.getComprobanteMotivoDePago().getFkTdoCodigo().getTdoDesc() + " Nº: " + dce.getComprobanteMotivoDePago().getDocNumero() + ";";
        }
        comprobanteEgreso.setComprobantes(cadenaComprobante);
        comprobanteEgreso.setListaDetallesComprobantesEgreso(detComegresoFacadeLocal.buscarPorComprobanteEgreso(comprobanteEgreso));
        Map parametros = new HashMap();
        List<ComprobanteEgreso> lista = new ArrayList<ComprobanteEgreso>();
        lista.add(comprobanteEgreso);
        nombreReporte = "voucherEgreso.jasper";
        JasperReport reporte = (JasperReport) JRLoader.loadObject(ruta + nombreReporte);
        JasperPrint jasperPrint = JasperFillManager.fillReport(reporte, parametros, new JRBeanCollectionDataSource(lista));
        JRExporter exporter = new JRXmlExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
        exporter.exportReport();
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
